package utils;


import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.picture_sharing_system.R;

public class PictureListAdapter extends CursorAdapter {

    private Context context;
    private LayoutInflater layoutInflater;

    public PictureListAdapter(Context context) {
        super(context, null, 0);
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View itemView = layoutInflater.inflate(R.layout.picture_list,
                parent, false);
        if (itemView != null) {
            ViewHolder vh = new ViewHolder();
            vh.imgs = itemView.findViewById(R.id.imgs);
            itemView.setTag(vh);

            return itemView;

        }

        return null;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder vh = (ViewHolder) view.getTag();
        String path = cursor.getString(cursor.getColumnIndex(
                MediaStore.Images.Media.DATA
        ));
        int position = cursor.getPosition();

        if (vh != null) {
            Glide.with(context).load(path).into(vh.imgs);
        }

    }

    public class ViewHolder {
        ImageView imgs;
    }
}
